package start.spring.basic.flowable.controller.flowable;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import start.spring.basic.flowable.common.SysConstant;
import start.spring.basic.flowable.controller.BaseController;
import start.spring.basic.flowable.entity.WfForm;
import start.spring.basic.flowable.service.flowable.IWfFormService;
import start.spring.basic.log.ServiceLog;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 
 * @author xuyy
 * @date 2023-08-22
 *
 */
//@RestController
//@RequestMapping("/form")
@Api(tags = "表单配置相关接口")
public class WfFormController extends BaseController {
	
    @Autowired
    private IWfFormService wfFormService;
	
    @ServiceLog(businessType = "查询表单配置列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("workflow:form:list")
    @PostMapping("/list")
    @ApiOperation(value = "查询表单配置列表", notes = "分页查询")
    public ResponsePage<WfForm> list(PageQuery pageQuery, @RequestBody WfForm wfForm) {
    	return super.returnResponse(this.wfFormService.selectWfFormPageList(pageQuery, wfForm));
    }
    
    @ServiceLog(businessType = "查询表单配置详情", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:list")
    @PostMapping("/detail")
    @ApiOperation(value = "查询表单配置详情", notes = "单条记录详情查询")
    public ResponseData<WfForm> detail(@RequestBody WfForm wfForm) {
    	PageResult<WfForm> pageResult = this.wfFormService.selectWfFormPageList(new PageQuery(), wfForm);
    	if(pageResult.getRecords()!=null&&!pageResult.getRecords().isEmpty()) {
    		WfForm detail = pageResult.getRecords().get(0);
    		return ResponseData.successData(detail);
    	}else {
    		return ResponseData.errorMsg("未查询到详细信息！");
    	}
    }
	
    @ServiceLog(businessType = "新增表单配置", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:add")
    @PostMapping("/add")
    @ApiOperation(value = "新增表单配置", notes = "")
    public ResponseData<Integer> add(@RequestBody WfForm wfForm) {
        return ResponseData.successData(this.wfFormService.insertWfForm(wfForm));
    }
	
    @ServiceLog(businessType = "修改表单配置", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:edit")
    @PostMapping("/edit")
    @ApiOperation(value = "修改表单配置", notes = "")
    public ResponseData<Integer> edit(@RequestBody WfForm wfForm) {
    	return ResponseData.successData(this.wfFormService.updateWfForm(wfForm));
    }
	
    @ServiceLog(businessType = "删除表单配置", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:remove")
    @PostMapping("/remove")
    @ApiOperation(value = "删除表单配置", notes = "")
    public ResponseData<Integer> remove(@RequestBody List<Long> formIds) {
    	return ResponseData.successData(this.wfFormService.deleteWfFormByFormIds(formIds));
    }
    
    @ServiceLog(businessType = "导出表单配置列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("workflow:form:export")
    @PostMapping("/export")
    @ApiOperation(value = "导出表单配置列表", notes = "")
    public void export(HttpServletResponse response, WfForm wfForm) {
        List<WfForm> list = this.wfFormService.selectWfFormList(wfForm);
        ExcelUtil<WfForm> util = new ExcelUtil<WfForm>(WfForm.class);
        util.exportExcel(response, list, "表单配置数据");
    }
}
